package com.xiewenyu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.mvc.Controller;


//public class HelloController implements Controller {
//
//    @Override
//    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
//        System.out.println("请求转发进行中");
//        ModelAndView mv=new ModelAndView();
//        mv.addObject("msg", "hello 你好");
//        mv.setViewName("hello");
//        return mv;
//    }
//
////    public String myFirstRequest() {
////        System.out.println("请求转发进行中");
////        return "hello2";  //此处是对视图解析器处进行了字符串拼接，全路径为 ：/WEB-INF/pages/success.jsp
////    }
//
//}

@Controller
public class HelloController {
    /**
     * 映射请求的名称：用于客户端请求；类似 Struts2 中 action 映射配置的 action 名称
     * 1. 使用 @RequestMapping 注解来映射请求的 URL
     * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 	InternalResourceViewResolver 视图解析器, * 会做如下的解析:
     * 		通过 prefix + returnVal + suffix 这	样的方式得到实际的物理视图, 然后做转发操作.
     * 		/WEB-INF/views/success.jsp
     */
    @RequestMapping("/hello")//声明 Bean 对象，为一个控制器组
    public String myFirstRequest(Model model) {
        model.addAttribute("msg", "中国人");
        System.out.println("请求转发进行中");
        return "hello";  //此处是对视图解析器处进行了字符串拼接，全路径为 ：/WEB-INF/pages/success.jsp
    }

    @RequestMapping("/hello2")//声明 Bean 对象，为一个控制器组
    public void myFirstRequest2( Model model, HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        model.addAttribute("msg", "中国人");
        System.out.println("请求转发进行中");
        rsp.getWriter().println("hel请求转发进行中");

    }

    @RequestMapping("hello3/{a}")
    public void myFirstRequest3(@PathVariable int a, HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        System.out.println("请求转发进行中");
        rsp.getWriter().println("PathVariable"+a);

    }

}
